package com.ichoice.rabbitmq.rpc.helper.sub;

import com.ichoice.rabbitmq.rpc.helper.consts.CommonContant;
import com.ichoice.rabbitmq.rpc.helper.model.SubScribeModel;
import com.ichoice.rabbitmq.rpc.helper.util.MqMetadataUtils;
import com.ichoice.rabbitmq.rpc.helper.util.StringUit;
import org.springframework.amqp.rabbit.core.RabbitTemplate;

/**
 * 订阅帮助类
 *
 * @author xianrensawang
 * @date 2021/3/11
 */
public final class SubScribeHelper {

    private SubScribeHelper() {

    }

    private RabbitTemplate rabbitTemplate;

    /**
     * 单例帮助类
     */
    private static class SubScribeSingle {
        private static final SubScribeHelper HELPER = new SubScribeHelper();
    }

    /**
     * 初始化消息队列
     *
     * @param rabbitTemplate 消息队列template
     */
    public static void setTemplate(RabbitTemplate rabbitTemplate) {
        SubScribeSingle.HELPER.rabbitTemplate = rabbitTemplate;
    }

    /**
     * 订阅  服务端
     *
     * @param serverName   服务端名称 applicationName
     * @param serverBean   服务端bean名称 交给spring IOC 管理的类
     * @param serverMethod 服务端bean里的方法
     * @param clientBean   客户端接收服务端发布 bean名称
     * @param clientMethod 客户端接收服务端发布bean里的方法名称
     */
    public static void subscribe(String serverName, String serverBean, String serverMethod,
                                 String clientBean, String clientMethod) {
        impl(serverName,serverBean,serverMethod,clientBean,clientMethod,"0");
    }

    /**
     * 移除订阅信息
     * @param serverName   服务端名称 applicationName
     * @param serverBean   服务端bean名称 交给spring IOC 管理的类
     * @param serverMethod 服务端bean里的方法
     * @param clientBean   客户端接收服务端发布 bean名称
     * @param clientMethod 客户端接收服务端发布bean里的方法名称
     */
    public static void remove(String serverName, String serverBean, String serverMethod,
                              String clientBean, String clientMethod) {
        impl(serverName,serverBean,serverMethod,clientBean,clientMethod,"1");
    }


    private static void impl(String serverName, String serverBean, String serverMethod,
                             String clientBean, String clientMethod, String del) {
        // fanout 交换机
        String exchange = CommonContant.EXCHANGE_DELAY;
        // 生成订阅信息类
        SubScribeModel scribeModel = new SubScribeModel();
        scribeModel.setServerName(serverName);
        scribeModel.setServerBean(StringUit.firstCharLower(serverBean));
        scribeModel.setServerMethod(serverMethod);
        scribeModel.setClientName(CommonContant.applicationName);
        scribeModel.setClientBean(StringUit.firstCharLower(clientBean));
        scribeModel.setClientMethod(clientMethod);
        scribeModel.setDel(del);
        // 广播到服务端所有的实例,缓存关系
        SubScribeSingle.HELPER.rabbitTemplate.convertAndSend(exchange, MqMetadataUtils.fanoutKey(serverName), scribeModel, msg -> {
            msg.getMessageProperties().setDelay(100);
            return msg;
        });
    }


    public static void subscribe(SubScribeModel scribeModel) {
        String serverName = scribeModel.getServerName();
        SubScribeSingle.HELPER.rabbitTemplate.convertAndSend(CommonContant.EXCHANGE_DELAY, MqMetadataUtils.fanoutKey(serverName), scribeModel, msg -> {
            msg.getMessageProperties().setDelay(60000);
            return msg;
        });
    }


}
